﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BolsaTrabajo.BusinessEntities;

namespace BolsaTrabajo.DataAccess
{
    public class CategoriaData
    {
        private String stConnection = String.Empty;

        public CategoriaData()
        {
            stConnection = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
        }

        public List<Categoria> ListarTodos()
        {
            SqlConnection cn = new SqlConnection(stConnection);
            SqlCommand cmd = cn.CreateCommand();
            cmd.CommandTimeout = 216000000;
            cmd.CommandText = "usp_Categoria_ListarCategoria";
            cmd.CommandType = CommandType.StoredProcedure;

            List<Categoria> lista = new List<Categoria>();
            try
            {
                cn.Open();
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    Categoria item = new Categoria();

                    item.IdCategoria = dr.GetInt32(dr.GetOrdinal("idCategoria"));
                    item.Nombre = dr.GetString(dr.GetOrdinal("nombre"));
                    item.IdEstado = dr.GetBoolean(dr.GetOrdinal("idEstado"));
                    lista.Add(item);

                    item = null;
                }
                dr.Close();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
                cn.Dispose();
            }
            return lista;
        }
    }
}
